package com.ygo.feihua;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ygo.feihua.view.ZdpImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.bumptech.glide.request.target.SimpleTarget;
import android.widget.ActionMenuView.LayoutParams;
import com.ygo.feihua.view.PinchImageView;
import com.ygo.feihua.util.*;
import com.bumptech.glide.request.transition.*;

public class ImageXiangxi extends Activity
{

	private PinchImageView image;// 自定义控件
	private Button xx_menu;
	Gongju gj;
	String pa=gj.PATH_GEN+"ygo助手/image/bc/";
	Bitmap bit;
	DialogUtils du;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_xiangxi);
		
		Intent intent=getIntent();
		String uri=intent.getStringExtra("uri");
		
		image=(PinchImageView) findViewById(R.id.image);
		xx_menu=(Button) findViewById(R.id.xx_menu);
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		Glide.with(this)
			.asBitmap()
			.load(Uri.parse(uri))
			
			.apply(Gongju.glideOption)
			.into((SimpleTarget)new SimpleTarget<Bitmap>(){

					  @Override
					  public void onResourceReady(Bitmap p1, Transition<? super Bitmap> p2)
					  {
						  bit=p1;
						  image.setImageBitmap(p1);
						  // TODO: Implement this method
					  }
			});
		xx_menu.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					ListView lv=(ListView) du.dialogl(false,"",new String[]{"保存到手机"});
					lv.setOnItemClickListener(new OnItemClickListener(){

							@Override
							public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
							{
					
								if(bit!=null){					
								switch(p3){
									case 0:
										SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyyMMddHHmmss");   
										Date curDate =  new Date(System.currentTimeMillis());//获取当前时间  
										String   str   =   formatter.format(curDate);
										boolean isbc=savePic(pa, str + ".jpg", bit);
										
										if (isbc)
										{
											gj.show("成功保存到" +pa+ str + ".jpg");
										}
										else
										{
											gj.show("保存失败");
										}
										du.dis();

										break;

								}
								}

							}

						});
					// TODO: Implement this method
				}
			});
			
	}

    // 保存到sdcard
    public  boolean savePic(String path,String name,Bitmap b)
	{

        FileOutputStream fos = null;
        try
		{
            File f = new File(path,name);
            if (f.exists())
			{
                f.delete();
            }

            fos = new FileOutputStream(f);
            if (null != fos)
			{
                b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                fos.flush();
                fos.close();
                return true;
            }
        }
		catch (FileNotFoundException e)
		{
            e.printStackTrace();
        }
		catch (IOException e)
		{
            e.printStackTrace();
        }
        return false;
    }

	
	
}
